﻿using AutoMapper;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Data;
using Microsoft.Extensions.Logging;
using ShanNiang.Mvvm.UiServices;
using ShanNiang.Service;
using ShanNiang.Service.Constants;
using ShanNiang.Service.Implementations;
using ShanNiang.Service.Interfaces;
using ShanNiang.Service.PackingModels.Requests;
using ShanNiang.Service.Scaner;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace ShanNiang.Mvvm.ViewModels
{
    /// <summary>
    /// 托码查询
    /// </summary>
    public partial class PackPalletHistoryViewModel : ScannableViewModel
    {
        #region 服务注入
        private readonly UserContext _userContext;
        private readonly IXkmService _xkmService;
        //private readonly Dispatcher _dispatcher;
        private readonly int _palletsPageSize = 20;
        #endregion
        public PackPalletHistoryViewModel(
            UserContext userContext,
            IXkmService xkmService,
            IUiService uiService,
            PackingConfig packingConfig, IScanerService scanerService, IMapper mapper, ILogger<ScannableViewModel> logger) : base(packingConfig, scanerService, uiService, mapper, logger)
        {
            _userContext = userContext;
            _xkmService = xkmService;
            _beginDate = DateTime.Today.AddMonths(-1);
            _endDate = DateTime.Today.AddDays(1);
            Pallets = new ObservableCollection<PalletViewModel>();
        }



        #region 字段和可观察属性
        /// <summary>
        /// 开始时间
        /// </summary>
        [ObservableProperty]
        private DateTime? _beginDate;
        /// <summary>
        /// 结束时间
        /// </summary>
        [ObservableProperty]
        private DateTime? _endDate;
        /// <summary>
        /// 当前页码
        /// </summary>
        [ObservableProperty]
        private int _pageIndex;
        /// <summary>
        /// 总页数
        /// </summary>
        [ObservableProperty]
        private int _totalPages;
        /// <summary>
        /// 当前企业
        /// </summary>
        private Guid? CurrentEnterpriseId
        {
            get
            {
                if (_userContext.UserResponse != null)
                {
                    return _userContext.UserResponse.EnterpriseId;
                }
                return null;
            }
        }
        public ObservableCollection<PalletViewModel> Pallets { get; set; }
        #endregion

        #region 可绑定 Command
        [RelayCommand]
        private async Task LoadPalletsAsync(FunctionEventArgs<int> parameter)
        {
            var request = new GetPalletsPageRequest(CurrentEnterpriseId);
            if (parameter != null)
            {
                PageIndex = parameter.Info;
            }
            request.PageIndex = PageIndex;
            request.PageSize = _palletsPageSize;
            request.BeginTime = BeginDate;
            request.EndTime = EndDate;
            var pageResponse = await _xkmService.GetPalletsPageAsync(request);
            if (pageResponse != null)
            {

                Pallets.Clear();
                foreach (var pallet in pageResponse.List)
                {
                    Pallets.Add(Mapper.Map<PalletViewModel>(pallet));
                }
                TotalPages = (pageResponse.TotalCount - 1) / _palletsPageSize + 1;

            }
        }
        #endregion

        #region 重写和事件处理
        protected override async Task LoadedAsync()
        {
            await base.LoadedAsync();
            if (TotalPages == 0)
            {
                await LoadPalletsAsync(new FunctionEventArgs<int>(1));
            }
        }
        protected override async void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            if (e.PropertyName == nameof(BeginDate) || e.PropertyName == nameof(EndDate))
            {
                await LoadPalletsAsync(new FunctionEventArgs<int>(1));
            }
        }
        protected override async Task CodeScanedAsync(object? sender, CodeEventArgs e)
        {
            await Task.CompletedTask;
            //throw new NotImplementedException();
        }
        #endregion

        #region 其他方法
        #endregion
    }
}
